package com.sumainfor.pc.gmio.controller.housekeeper;

import com.alibaba.fastjson.JSONObject;
import com.sumainfor.common.config.RedisUtils;
import com.sumainfor.common.util.*;
import com.sumainfor.common.utlis.CompuUtils;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisHash;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

/**
 * @author Meng
 * @ClassName HoMeterBillController
 * @date 2019/12/5 13:51
 * @description: TODO
 */
@Controller
@RequestMapping("/HoMeterBill")
public class HoMeterBillController {

    @Autowired
    RedisUtils redisUtils;

    /**
     * 查询智能设备余额
     * @param params
     * @return
     */
    @ResponseBody
    @PostMapping("/MeterBalance")
    public JsonResult MeterBalance(@RequestBody Map<String,Object> params){

        JsonResult result = new JsonResult();




//                参数格式
//                ProOperaType
//                HouInfoId
//                Intelligent
//                list[
//
//
//                            {
//                            "HouConType": 1,
//                            "Identifier": "MG2309433",
//                            "HouConName": "公共区域",
//                            "Intelligent": 0,
//                            "HouConId": "FY-201911280002Z",
//                            "PalHouId": "FYPZ-201911280005"
//                            },
//
//                        ]







           List<Map<String,Object>> InteDeviceBeeList = (List<Map<String, Object>>) params.get("list");
                if (InteDeviceBeeList.size()>0){
                    System.out.println("接收到的参数:" + params);
                    if (params.get("Intelligent").toString().equals("0")) {
                        String InteDeviceBeeToken = redisUtils.get("InteDeviceBeeToken");
                        System.out.println(InteDeviceBeeToken);
                        //如果获取不到就重新登录
                        if (InteDeviceBeeToken == null) {
                            Map<String, Object> loginMap = new HashMap<String, Object>();
                            loginMap.put("version", UUIDUtils.generateShortUuid());//业务流水号UUID
                            loginMap.put("appid", InteDeviceBeeConfig.APP_ID);
                            //登录获取token
                            loginMap = InteDeviceBeeUtil.userLogin(InteDeviceBeeConfig.USERID, InteDeviceBeeConfig.PASS, loginMap);
                            if (loginMap.get("Code").toString().equals("0")) {//0 状态码 请求成功
                                InteDeviceBeeToken = loginMap.get("Expand").toString();
                                //token存到redis中 蜂电token 有效时长一小时
                                redisUtils.set("InteDeviceBeeToken", InteDeviceBeeToken, 3600);
                                System.out.println(InteDeviceBeeToken);
                            } else {
                                return result.putFailed("token获取失败");
                            }
                        }

                        //智能电表必定是每个房间都有一个电表，用房间集合的长度初始化智能电表设备编号数组
                        List<String> InteList = new ArrayList<String>();
                        Map<String, Object> headMap = new HashMap<String, Object>();
                        headMap.put("uid", InteDeviceBeeConfig.UID);//唐巢在蜂电用户ID
                        headMap.put("token", InteDeviceBeeToken);//访问令牌
                        headMap.put("version", "0116010101");//版本号默认0116010101
                        for (int i = 0; i < InteDeviceBeeList.size(); i++) {
                            Map<String, Object> map1 = InteDeviceBeeList.get(i);
                            if (map1.get("Identifier") != null) {
                                InteList.add(map1.get("Identifier").toString());
                            }
                        }

                        //蜂电返回的参数
                        Map<String, Object> InteDataMap = InteDeviceBeeUtil.deviceAmmeterPcode(InteList, headMap);
                        if (InteDataMap.get("Code").toString().equals("0")) {// 0蜂电接口返回正常
                            //电表数组
                            List<Map<String, Object>> InteDataList = (List<Map<String, Object>>) InteDataMap.get("Data");
                            for (Map<String, Object> map1 : InteDeviceBeeList) {
                                if (map1.get("Identifier") != null) {
                                    for (Map<String, Object> map2 : InteDataList) {
                                        if (map2.get("Ccode") != null) {
                                            if (map1.get("Identifier").toString().equals(map2.get("Pcode").toString())) {
                                                map1.put("Uuid", map2.get("Uuid"));
                                                map1.put("Price",map2.get("Price"));
                                                map1.putAll(map2);
                                            }
                                        }
                                    }
                                }
                            }
                            System.out.println(InteDeviceBeeList);

                            String Hous = MapUtils.getString(params, "HouInfoId");
                            String HouLast = Hous.substring(Hous.length() - 1, Hous.length());
                            if (MapUtils.getInteger(params, "ProOperaType") == 1) {//合租,并且房间智能电表为蜂电,查询余额
                                for (Map<String, Object> ceshiMap : InteDeviceBeeList) {
                                    String HouConName = MapUtils.getString(ceshiMap, "HouConName");
                                    String HouConLast = HouConName.substring(HouConName.length() - 1, HouConName.length());
                                    if (HouLast.equals(HouConLast)) {

                                      Map<String,Object> ExpandMap = (Map<String, Object>) ceshiMap.get("Expand");
                                        ceshiMap.put("SmartBalance",CompuUtils.mul(MapUtils.getDouble(ceshiMap,"Price"),MapUtils.getDouble(ExpandMap,"surplus")));
                                        result.put(ceshiMap);
                                    }
                                }
                            }
                        } else {
                            return result.putFailed("第三方服务器繁忙");
                        }

                    }
                    else if (params.get("Intelligent").toString().equals("1")) {//大华
                        String InteDaHuaToken = redisUtils.get("InteDaHuaToken");
                        //如果获取不到就重新登录
                        if (InteDaHuaToken == null) {
                            /*获取token*/
                            Map<String, Object> DaParams = new HashMap<String, Object>();
                            DaParams.put("pw", InteDeviceDaHuaConfig.PW);
                            DaParams.put("userName", InteDeviceDaHuaConfig.USERNAME);
                            DaParams = InteDeviceDaHuaUtil.getToken(DaParams);
                            if (DaParams.get("code") != null && DaParams.get("code").toString().equals("1")) {//1 状态码 请求成功
                                InteDaHuaToken = DaParams.get("data").toString();
                                //token有效期2个小时，所有接口Code=0 ，token失效 需要重新获取
                                redisUtils.set("InteDaHuaToken", InteDaHuaToken, 3600 * 2);
                                System.out.println(InteDaHuaToken);
                            } else {
                                return result.putFailed("token获取失败");
                            }
                        }

                        String Hous = MapUtils.getString(params, "HouInfoId");
                        String HouLast = Hous.substring(Hous.length() - 1, Hous.length());

                        if (MapUtils.getInteger(params, "ProOperaType") == 1) {//合租查询余额
                            for (Map<String, Object> ceshiMap : InteDeviceBeeList) {
                                String HouConName = MapUtils.getString(ceshiMap, "HouConName");
                                String HouConLast = HouConName.substring(HouConName.length() - 1, HouConName.length());

                                if (HouLast.equals(HouConLast)) {
                                    Map<String, Object> tokenMap = new HashMap<>();
                                    tokenMap.put("token", InteDaHuaToken);
                                    tokenMap.put("meter_id", ceshiMap.get("Identifier"));
                                    Map<String, Object> getParams = InteDeviceDaHuaUtil.getRemainById(tokenMap);
                                    if (getParams.get("code") != null && getParams.get("code").toString().equals("1")) {//0 状态码 请求成功

                                       if (MapUtils.getString(ceshiMap,"Identifier").equals(MapUtils.getString(getParams,"meter_id"))){
                                           ceshiMap.putAll(getParams);
                                           ceshiMap.put("SmartBalance", CompuUtils.mul(MapUtils.getDouble(ceshiMap,"data"),MapUtils.getDouble(ceshiMap,"price")));

                                       }
                                  result.put(ceshiMap);

                                    }else {
                                        return result.putFailed("第三方服务器繁忙");

                                    }
                                }
                            }
                        }
                    }

                }else{
                    return result.putFailed("参数错误");
                }

               return result;
    }


    }

